local json = require "cjson"
local config = require "config"

SERVICE_D:register(config.server.game_channel, function (linda, data)
    local recv = json.decode(data)
    if not recv then
        spdlog.warn("game_channel", string.format("parse error, data : %s", data));
        return
    end

    local clientAddr = recv.Sender
    if not clientAddr then
        spdlog.warn("game_channel", string.format("member[Sender] type error, data : %s", data));
        return
    end

    local clientId = recv.userName
    if not clientId or string.len(clientId) <= 0 then
        spdlog.warn("game_channel", string.format("member[userName] type error, data : %s", data));
        return
    end

    local userOb = LOGIN_D:find_user(clientId)
    if not userOb then
        spdlog.error("game_channel", string.format("not found userOb, clientId [%s] MsgType [%s]", clientId, recv.MsgType))
        LOGIN_D:kickout(clientAddr, clientId)
        return
    end

    if not recv.SendTime then
        spdlog.warn("game_channel", string.format("member[SendTime] type error, data : %s", data));
        return
    end

    if recv.SendTime + 60 < os.time() then
        spdlog.warn("game_channel", string.format("member[SendTime] too long, data : %s", data));
        return
    end

    if not recv.MsgData then
        spdlog.warn("game_channel", string.format("member[MsgData] type error, data : %s", data));
        return
    end
    local MsgData = {}
    if recv.MsgData ~= "" then
        local ok
        ok, MsgData = pcall(json.decode, recv.MsgData)
        if not ok then
            spdlog.warn("game_channel", string.format("member[MsgData] parse error, data : %s", data));
            return
        end
    end

    COMMAND_D:process_command(userOb, recv.MsgType, MsgData)
end)
